package com.protocols.sctp.m3ua.sccp.tcap.map;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Arrays;

import com.Main.MyServiceInterface;
import com.asterisk.AmiManagerInterface;
import com.protocols.GeneralProtocolData;

public class ChatServiceHandler extends java.rmi.server.UnicastRemoteObject implements MyServiceInterface {
	
	private String MSISDN="";
	private String IMSI="";
	private int LastInvokeID=80;
	private int DataCodingScheme=0;
	private String USSDCode="";
	private String HomeGT="";
	private String HLRGT="";	
	private long HomeTransactionID=0;
	private long HLRTransactionID=0;
	private MapInvokeUnstructuredSSNotify UssdNotify= new MapInvokeUnstructuredSSNotify();
	private MapInvokeUnstructuredSSRequest UssdRequest= new MapInvokeUnstructuredSSRequest();
	private Registry Registry;
	private int RegistryPort=0;
	private MapHandlerInterface MapHandler=null;
	
	public ChatServiceHandler(GeneralProtocolData ussdprocessRequest, GeneralProtocolData mapopenPdu,int OPC, int DPC, String OGT, String DGT, long OrigTransactionID, long DestTransactionID, int registryPort) throws RemoteException {
		
		super();
		MapInvokeProcessUSSRequest UssdProcesReq= (MapInvokeProcessUSSRequest)ussdprocessRequest;
		MapOpen mapopen = (MapOpen)mapopenPdu;
		
		MSISDN=UssdProcesReq.getMsisdn();
		IMSI=mapopen.getDestinationReferenceNumber();
		
		DataCodingScheme=(int)UssdProcesReq.getDataCodingScheme();
		USSDCode=UssdProcesReq.getUSSDString();
		HLRGT=OGT;
		HomeGT=DGT;
		HomeTransactionID=DestTransactionID;
		HLRTransactionID=OrigTransactionID;
		
		UssdRequest.setDataCodingScheme(0, 15);
		UssdRequest.setAlertingPattern(0);		
		UssdNotify.setDataCodingScheme(0, 15);
		RegistryPort=registryPort;
		
	}
	
	
	
	private void sendUSSDNotification(String ussdString){
		
		try{
			MapHandler.sendUSSDNotify(HomeTransactionID, DataCodingScheme, ussdString, 0, MSISDN, 30000);
			
		}catch(RemoteException e){
			//Log.severe(HomeTransactionID+":Exception Occured :" +e.getClass().getSimpleName()+":"+ Arrays.toString(e.getStackTrace()));
			System.out.println(HomeTransactionID+":Exception Occured :" +e.getClass().getSimpleName()+":"+ Arrays.toString(e.getStackTrace()));

		}catch(Exception e){
			//Log.severe(HomeTransactionID+":Exception Occured :" +e.getClass().getSimpleName()+":"+ Arrays.toString(e.getStackTrace()));
			System.out.println(HomeTransactionID+":Exception Occured :" +e.getClass().getSimpleName()+":"+ Arrays.toString(e.getStackTrace()));
		}
		
	}
	
	
	private String sendUSSDRequest(String ussdString){
		try{
			String response =MapHandler.sendUSSDRequest(HomeTransactionID, DataCodingScheme, ussdString, 0, MSISDN, 30000);
			if(response==null){
				
			}
			
			return response;
		}catch(RemoteException e){
			//Log.severe(HomeTransactionID+":Exception Occured :" +e.getClass().getSimpleName()+":"+ Arrays.toString(e.getStackTrace()));
			System.out.println(HomeTransactionID+":Exception Occured :" +e.getClass().getSimpleName()+":"+ Arrays.toString(e.getStackTrace()));

		}catch(Exception e){
			//Log.severe(HomeTransactionID+":Exception Occured :" +e.getClass().getSimpleName()+":"+ Arrays.toString(e.getStackTrace()));
			System.out.println(HomeTransactionID+":Exception Occured :" +e.getClass().getSimpleName()+":"+ Arrays.toString(e.getStackTrace()));
		}
		
		
		
		return "";
	}


	
	private void runLogic(){
		System.out.println("Executing the Logic");
		String resp=sendUSSDRequest("It's havin Mal-chractor at the end.");
		sendUSSDNotification(resp);
	}

	
	@Override
	public void startLayer() throws Exception {
		initialise();
		establishMapConnection();
		runLogic();
		
	}
	
	private void establishMapConnection() throws InterruptedException{
		
		Boolean retry=true;				
		while(retry)
		{
			try{
				MapHandler=(MapHandlerInterface)(Registry.lookup("MapHandler"));
				retry=false;
			}catch(Exception e){	
				System.out.println("Couldn't Obtained the AmiManager. Retry in "+3000+" Secs."+":Exception Occured :" +e.getClass().getSimpleName()+":"+ Arrays.toString(e.getStackTrace()));
				Thread.sleep(3000);
			}
		}				
		System.out.println("Obtained MAP Instance");
	}

	
	
	private void initialise() throws Exception{
		
		try{
			Registry = LocateRegistry.createRegistry( RegistryPort );
			//if(logInfoEnabled)Log.info("Created Registry on Port:"+layerServiceport);
		}catch(RemoteException e){
			Registry = LocateRegistry.getRegistry( RegistryPort );	
			//if(logInfoEnabled)Log.info("Obtained Registry from Port:"+layerServiceport);
		}
			
		Registry.rebind(String.valueOf(HomeTransactionID), this);			
		//if(logInfoEnabled)Log.info("Bound Tcap Handler on Url:"+camelhandlerurl);
		
	}
	

}
